package com.haltakov.letterjumble.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.haltakov.letterjumble.Messages;
import com.haltakov.letterjumble.game.api.Dictionary;

public class DictionaryDialog extends JDialog {

	private final Dictionary dictionary;

	public DictionaryDialog (Dictionary dictionary) {

		super();

		this.dictionary = dictionary;

		setLayout(new GridLayout(4,1));
		setPreferredSize(new Dimension(400,250));

		//FIRST ROW - WORD
		JPanel firstRowPanel = new JPanel(new FlowLayout());
		firstRowPanel.setPreferredSize(new Dimension(400, 80));
		firstRowPanel.setLayout(new FlowLayout());

		JLabel wordLabel = new JLabel(Messages.getString("DictionaryDialog.word")); //$NON-NLS-1$
		firstRowPanel.add(wordLabel);

		final TextField wordField = new TextField(Messages.getString("DictionaryDialog.type")); //$NON-NLS-1$
		firstRowPanel.add(wordField);

		add(firstRowPanel);

		//SECOND ROW - MESSAGE
		final JPanel secondRowPanel = new JPanel(new FlowLayout());
		secondRowPanel.setPreferredSize(new Dimension(400, 40));

		final JPanel colorPanel = new JPanel();
		final JLabel messageLabel = new JLabel();
		colorPanel.add(messageLabel);
		secondRowPanel.add(colorPanel);

		add(secondRowPanel);

		//THIRD ROW - BUTTONS
		JPanel thirdRowPanel = new JPanel(new FlowLayout());
		thirdRowPanel.setPreferredSize(new Dimension(400, 80));
		thirdRowPanel.setLayout(new FlowLayout());

		JButton evaluateButton = new JButton(Messages.getString("DictionaryDialog.check")); //$NON-NLS-1$
		thirdRowPanel.add(evaluateButton);

		evaluateButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				if(DictionaryDialog.this.dictionary.checkWord(wordField.getText().toUpperCase()) == false){
					messageLabel.setText(Messages.getString("DictionaryDialog.notvalid")); //$NON-NLS-1$
					colorPanel.setBackground(Color.RED);
				}
				else {
					messageLabel.setText(Messages.getString("DictionaryDialog.valid")); //$NON-NLS-1$
					colorPanel.setBackground(Color.GREEN);
				}
			}
		});


		JButton addWordButton = new JButton(Messages.getString("DictionaryDialog.add")); //$NON-NLS-1$
		thirdRowPanel.add(addWordButton);

		addWordButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				DictionaryDialog.this.dictionary.addWord(wordField.getText().toUpperCase());
				messageLabel.setText(Messages.getString("DictionaryDialog.success")); //$NON-NLS-1$
				colorPanel.setBackground(secondRowPanel.getBackground());
			}  
		});

		add(thirdRowPanel);

		//FOURTH ROW - OK BUTTON
		JPanel fourthRowPanel = new JPanel(new FlowLayout());
		fourthRowPanel.setPreferredSize(new Dimension(400, 50));

		final JButton okButton = new JButton(Messages.getString("DictionaryDialog.ok")); //$NON-NLS-1$
		fourthRowPanel.add(okButton);

		okButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				DictionaryDialog.this.setVisible(false);
			}
		});

		add(fourthRowPanel);

		setTitle(Messages.getString("DictionaryDialog.dictionary")); //$NON-NLS-1$

		pack();
	}

}